package com.bj.disc.ui.login.utils;

import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * 
 * @author chenfangyi
 *
 */
public class KeyBoardHelper {
	/**
     * 关闭虚拟键盘
     * @param aContext  程序上下文
     * @param aEdiText  当前对应输入框
     */
	public static void hideKeyBoard(final Context aContext,
			final EditText aEdiText) {
		if (null == aEdiText || null == aContext) {
			return;
		}
		final InputMethodManager imm = (InputMethodManager) aContext
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (aEdiText != null) {
			aEdiText.post(new Runnable() {

				@Override
				public void run() {
					imm.hideSoftInputFromWindow(aEdiText.getWindowToken(), 0);
				}
			});
		}
	}

	/**
	 * 立即关闭虚拟键盘
	 * @param aContext  程序上下文
	 * @param aEdiText  当前对应输入框
	 */
	public static void hideKeyBoardNotPost(final Context aContext,
									final EditText aEdiText) {
		if (null == aEdiText || null == aContext) {
			return;
		}
		final InputMethodManager imm = (InputMethodManager) aContext
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (aEdiText != null) {
			imm.hideSoftInputFromWindow(aEdiText.getWindowToken(), 0);
		}
	}
    
    /**
     * 打开虚拟键盘
     * @param aContext  程序上下文
     * @param aEdiText  当前对应输入框
     */
	public static void showKeyBoard(final Context aContext,
			final EditText aEdiText) {
		if (null == aEdiText || null == aContext) {
			return;
		}
		final InputMethodManager imm = (InputMethodManager) aContext
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (aEdiText != null) {
			aEdiText.requestFocus();
			aEdiText.postDelayed(new Runnable() {

				@Override
				public void run() {
					imm.showSoftInput(aEdiText, 0);
				}
			}, 250);
		}
	}
	/**
     * 无延迟打开虚拟键盘
     * @param aContext  程序上下文
     * @param aEdiText  当前对应输入框
     */
	public static void showKeyBoardWithoutDelay(final Context aContext,
			final EditText aEdiText) {
		if (null == aEdiText || null == aContext) {
			return;
		}
		final InputMethodManager imm = (InputMethodManager) aContext
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (aEdiText != null) {
			aEdiText.requestFocus();
			aEdiText.post(new Runnable() {

				@Override
				public void run() {
					imm.showSoftInput(aEdiText, 0);
				}
			});
		}
	}
}
